package com.leetroll.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leetroll.api.mapper.OrderDetailMapper;
import com.leetroll.api.service.FileService;
import com.leetroll.api.service.ItemService;
import com.leetroll.api.service.OrderDetailService;
import com.leetroll.common.constant.SQLConstant;
import com.leetroll.common.entity.Item;
import com.leetroll.common.entity.OrderDetail;
import com.leetroll.common.vo.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderDetailServiceImpl extends SuperServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    @Override
    public List<OrderDetailVO> listByOrderId(Long orderId) {
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.ORDER_ID, orderId);
        List<OrderDetail> details = this.list(queryWrapper);
        return details.stream().map(this::makeOrderDetailVO).collect(Collectors.toList());
    }

    @Override
    public OrderDetail getByOrderIdAndItemId(List<Long> orderIds, Integer itemId) {
        if (orderIds == null || orderIds.size() == 0 ) return null;
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SQLConstant.ORDER_ID, orderIds);
        queryWrapper.eq(SQLConstant.ITEM_ID, itemId);
        return this.getOne(queryWrapper);
    }

    private OrderDetailVO makeOrderDetailVO(OrderDetail orderDetail) {
        OrderDetailVO detailVO = new OrderDetailVO();
        detailVO.setItemId(orderDetail.getItemId());
        detailVO.setPrice(orderDetail.getPrice());
        detailVO.setActualPrice(orderDetail.getActualPrice());
        return detailVO;
    }
}
